package com.ruoyi.project.im.users.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.im.doyuming.domain.Doyuming;
import com.ruoyi.project.im.doyuming.mapper.DoyumingMapper;
import com.ruoyi.project.im.groups.domain.Groups;
import com.ruoyi.project.im.groups.service.IGroupsService;
import com.ruoyi.project.im.personhost.domain.Personhost;
import com.ruoyi.project.im.personhost.mapper.PersonhostMapper;
import com.ruoyi.project.im.users.domain.Tongji;
import com.ruoyi.project.im.utils.RandomNum;
import com.ruoyi.project.system.tongji.domain.Userexpert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.im.users.mapper.UsersMapper;
import com.ruoyi.project.im.users.domain.Users;
import com.ruoyi.project.im.users.service.IUsersService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 用户管理Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-12
 */
@Service
public class UsersServiceImpl implements IUsersService
{
    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private DoyumingMapper doyumingMapper;

    @Autowired
    private PersonhostMapper personhostMapper;

    @Autowired
    private IGroupsService groupsService;

    /**
     * 查询用户管理
     *
     * @param userId 用户管理主键
     * @return 用户管理
     */
    @Override
    public Users selectUsersByUserId(String userId)
    {
        return usersMapper.selectUsersByUserId(userId);
    }

    @Override
    public Users selectUsersByKefuIdWithIP(Users users){
        return usersMapper.selectUsersByKefuIdWithIP(users);
    }

    /**
     * 查询用户管理列表
     *
     * @param users 用户管理
     * @return 用户管理
     */
    @Override
    public List<Users> selectUsersList(Users users)
    {
        return usersMapper.selectUsersList(users);
    }

    @Override
    public int insertUsersBykami(Users users) {
        return usersMapper.insertUsers(users);
    }

    /**
     * 新增用户管理
     *
     * @param users 用户管理
     * @return 结果
     */
    @Override
    public int insertUsers(Users users)
    {
        users.setCreateTime(DateUtils.getNowDate());
        int i =usersMapper.insertUsers(users);
        if(i>0){ //成功
            updateUsersByOpration(users);
        }
        return  i;
    }


    @Override
    public void updateUsersByOpration(Users users) {
        Doyuming rkym=doyumingMapper.selectDoyumingBYOne("1");//入口域名
        Doyuming ldym=doyumingMapper.selectDoyumingBYOne("4");//落地域名
        Personhost rkPerson=new Personhost();
        rkPerson.setLeixing(1L);
        rkPerson.setShortcode(RandomNum.randomxiaoxie(4));
        rkPerson.setUrl("http://"+ RandomNum.randomxiaoxie(10)+"."+rkym.getDomain());
        rkPerson.setStatus(2l);
        rkPerson.setUserId(users.getUserId());
        int rknum=personhostMapper.insertPersonhost(rkPerson);
        if(rknum>0){
            rkym.setFenpeicount(rkym.getFenpeicount()+1);
            doyumingMapper.updateDoyuming(rkym);

        }
        Personhost ldPerson=new Personhost();
        ldPerson.setLeixing(4L);
        ldPerson.setUrl("http://"+ RandomNum.randomxiaoxie(10)+"."+ldym.getDomain());
        ldPerson.setStatus(2l);
        ldPerson.setUserId(users.getUserId());
        int ldnum=personhostMapper.insertPersonhost(ldPerson);
        if(ldnum>0){
            ldym.setFenpeicount(ldym.getFenpeicount()+1);
            doyumingMapper.updateDoyuming(ldym);
        }

        //建群
//        Groups  groups=new Groups();
//        groups.setCreatorUserId(users.getUserId());
//        groups.setName("客户群");
//        groups.setIntroduction("说些什么呢!");
//        groups.setNotification("暂无公告!");
//        groupsService.insertGroups(groups);
    }

    /**
     * 修改用户管理
     *
     * @param users 用户管理
     * @return 结果
     */
    @Override
    public int updateUsers(Users users)
    {
        return usersMapper.updateUsers(users);
    }

    /**
     * 批量删除用户管理
     *
     * @param userIds 需要删除的用户管理主键
     * @return 结果
     */
    @Override
    public int deleteUsersByUserIds(String userIds)
    {
        return usersMapper.deleteUsersByUserIds(Convert.toStrArray(userIds));
    }

    /**
     * 删除用户管理信息
     *
     * @param userId 用户管理主键
     * @return 结果
     */
    @Override
    public int deleteUsersByUserId(String userId)
    {
        return usersMapper.deleteUsersByUserId(userId);
    }

    @Override
    public List<Users> selectUsersListByEnd() {
        return usersMapper.selectUsersListByEnd();
    }

    @Override
    public List<Users> selectUsersListByEndwithup(String account) {
        return usersMapper.selectUsersListByEndwithup(account);
    }

    @Override
    public List<Userexpert> selectUsersListBykf(String kamicode, Date tongjidate) {
        return usersMapper.selectUsersListBykf(kamicode,tongjidate);
    }

    @Override
    public Users selectUsersByKamicode(String kamicode) {
        return usersMapper.selectUsersByKamicode(kamicode);
    }

    @Override
    public Long selectUsersByUserIdcount(String userId) {
        return usersMapper.selectUsersByUserIdcount(userId);
    }

    @Override
    public int selectUsersByUpGetTotal(List list) {
        return usersMapper.selectUsersByUpGetTotal(list);
    }

    @Override
    public List<Tongji> selectTongjiuserByKamicode(String kamicode,String userId) {
        return usersMapper.selectTongjiuserByKamicode(kamicode,userId);
    }
}
